﻿/*

Copyright 2010 YiqYaq LLC
Author: Dale Low (gumbypp@yahoo.com)
 
Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 
 
    http://www.apache.org/licenses/LICENSE-2.0 
    
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 

The above license does not apply to the RadioWeave logo and name artwork,
examples of which are only included in this project for illustration
purposes.

*/

/**
 * \brief Common utility functions
 * \ingroup chumby
 */
class Utils
{
    private static var fLogs:Array = new Array();
    
    /**
        \brief Log a message to the debug console with the current date/time.
               Messages will also be stored in a local buffer
               (max. 100 entries) for later retrieval with \ref getLogs.
        \param msg log message
     */
    public static function log(msg):Void
    {
        var d:Date = new Date();
        
        msg = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + " " + msg;
        trace(msg);
        fLogs.push(msg);
		
		// store max of 100 entries
		if (fLogs.length > 100)
			fLogs.shift();
    }
    
    /**
        \return string of all accumulated logs, up to the maximum.
     */
    public static function getLogs():String
    {
		var ret:String = "";		
		for (var i:Number=0; i<fLogs.length; i++)
			ret += fLogs[i] + "\n";
			
		return ret;
    }

    /**
        \brief Recursively dump the contents of an object.
        \return object string
     */
	public static function dumpObject(obj, indent):String
	{
		if (indent == null) 
		{
			indent = "";
		}
		var out = "";
		for (var item in obj) 
		{
			if (typeof (obj[item]) == "object") 
			{
				out += indent+"["+item+"] => Object\n";
			} 
			else 
			{
				out += indent+"["+item+"] => "+obj[item]+"\n";
			}
			out += Utils.dumpObject(obj[item], indent+"   ");
		}
		return out;
	}
	
    /**
        \brief Search for a specific key in an object.
        \return true if found, false otherwise
     */
	public static function findInObject(id:String, obj:Object):Boolean
	{
		var key;
		
		for (key in obj)
		{
			if (key == id)
				return true;
		}
		
		return false;
	}
	
    /**
        \brief Scroll a text field to fit the specified width.  
        \return timer ID that can be passed to clearInterval or 0 if no scrolling is required.
     */
	public static function scrollTextField(tf:TextField, originalText:String, maxWidth:Number):Number
	{
    	tf.text = originalText;
		if (tf.textWidth <= maxWidth)
			return 0;
			
		var maxScrollCount:Number = 0;
		while (tf.textWidth > maxWidth)
		{
			//log("scrollCount=" + maxScrollCount + ", tf.textWidth=" + tf.textWidth);
			
			++maxScrollCount;
			tf.text = originalText.substr(maxScrollCount);
		}
		
		// how many chars can we display in the specified width (approx)
		var maxLen:Number = originalText.length - maxScrollCount;
		var scrollCount:Number = 0;
		return setInterval(function()
							{
								if (++scrollCount >= maxScrollCount)
									scrollCount = 0;
									
								tf.text = originalText.substr(scrollCount, maxLen);
							}, 100);
	}	
}

